layui.use(['form', 'table', 'laydate'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        $ = layui.$,
        laydate = layui.laydate;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    //初始化
    $(function(){
        var id = $("#id").val();
        if(id != '' && id != null && id != undefined){
            var index = layer.load(2);
            var url = "/web/boLoveList/getInfoById";
            $.get(url, {id: id}, function(data){
                layer.close(index);
                if(data.state == "200"){
                    $("#createDate").val(data.createDate);
                    $("#userName").val(data.userName);
                    $("#content").val(data.content);
                    $('#heSweet').val(data.heSweet);
                    $('#herSweet').val(data.herSweet);
                    $('#reson').val(data.reson);
                    for(var i = 0; i < $("input[name='heSatisfied']").length; i++){
                        if(data.heSatisfied == $($("input[name='heSatisfied']")[i]).val()){
                            $($("input[name='heSatisfied']")[i]).attr("checked", "checked");
                            break;
                        }
                    }
                    for(var i = 0; i < $("input[name='herSatisfied']").length; i++){
                        if(data.herSatisfied == $($("input[name='herSatisfied']")[i]).val()){
                            $($("input[name='herSatisfied']")[i]).attr("checked", "checked");
                            break;
                        }
                    }
                    for(var i = 0; i < $("input[name='status']").length; i++){
                        if(data.status == $($("input[name='status']")[i]).val()){
                            $($("input[name='status']")[i]).attr("checked", "checked");
                            break;
                        }
                    }
                }
                form.render();
            })
        }
    });

    //他满意度改变
    form.on('radio(heSatisfied)', function(data){
        $("#heSweet").val(parseInt(data.value) + 1);
    });

    //她满意度改变
    form.on('radio(herSatisfied)', function(data){
        $("#herSweet").val(parseInt(data.value) + 1);
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var index = layer.load(2);
        var url = "/web/boLoveList/save";
        $.post(url, data.field, function(res){
            if(res.state == "200"){
                layer.msg(res.message, {icon:1});
            }else{
                layer.msg(res.message, {icon:2});
            }
            // 关闭弹出层
            layer.close(index);
            layer.close(parentIndex);
        }, 'json');

        return false;
    });


    $("#closeBtn").on("click", function(){
        // 关闭弹出层
        layer.close(parentIndex);
    });

});